﻿using System.Web.Mvc;
using NxPartyAffairsOpen.Models;
using NxPartyAffairsOpen.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NxPartyAffairsOpen.Web.Controllers
{
    public class PortalController : Controller
    {
        private NXDBDataContext db = new NXDBDataContext();

        

        public ActionResult Index()
        {

            PicNewModel pim = CategoryHelper.GetPicNews(Config.LowCategoryID,  "pic1");
            pim.CategoryID = Config.DangWuFengCaiID;
            pim.DisplayName = "图说南翔";

            ViewData["tushuonaxiang"] = pim;

            ViewData["PicNews"] = CategoryHelper.GetPicNews(Config.TopCategoryID, true, "1");

            ViewData["Lists"] = CategoryHelper.GetList(Config.MiddleCategoryIDs);


            return View();
        }

        public ActionResult InfoList(Guid? categoryid, int? pi, int? year)
        {
            ViewData["DisplayName"] = "栏目";
            if (categoryid.HasValue)
            {
                Category cate = db.Category.SingleOrDefault(l => l.CategoryID == categoryid.Value);
                if (cate != null)
                    ViewData["DisplayName"] = cate.DisplayName;

            }

            Guid gd = categoryid.HasValue ? categoryid.Value : Guid.Empty;

            int count = 0;
            InformationListModel ilm = new InformationListModel();
            ilm.PageIndex = pi.HasValue ? pi.Value : 0;


            List<InformationModel> infoList = new List<InformationModel>();
            string where = string.Empty;
            ilm.Year = DateTime.Now.Year;
            if (year.HasValue)
            {
                ilm.Year = year.Value;
                if (ilm.Year < 1998 || year > 2200)
                {
                    ilm.Year = DateTime.Now.Year;
                }

            }
            where = "ShowTime>='" + ilm.Year + "-1-1 0:0:0' and ShowTime <='" + ilm.Year + "-12-31 0:0:0'";
            infoList = CategoryHelper.GetInfoList(Config.AppID, gd, 35, ilm.PageIndex, "ShowTime", ref count, where, true, true, 0);

            ilm.TotalCount = count;
            ilm.InformationList = infoList;

            return View(ilm);
        }
        public ActionResult Details(Guid? id)
        {
            InformationModel info = InformationHelper.GetInformation(db, id.HasValue ? id.Value : Guid.Empty);
            if (info == null)
                info = new InformationModel();

            return View(info);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
